import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

public class test {

	
	public static void main(String[] args)
	{
		String inputString ="";
		String pattern = "";
		if(args.length != 3)
		{
			System.out.println("The program needs 3 arguments:");
			System.out.println("Argument 1 should be either KMP or BM");
			System.out.println("Argument 2 should be the file containing the string(placed in ..//files/)");
			System.out.println("Argument 3 should be the pattern to search for");
			return;
		}
		
		pattern = args[2];
		if(!args[1].endsWith(".txt"))
		{
			try {
				inputString = powpow(args[1].concat(".txt"));
			} catch (IOException e) {
				// TODO Auto-generated catch block
				System.out.println("Could'nt find the input file. Are you sure, you placed it in ..//files/?");
				return;
			}
		}
		else
		{
			try {
				inputString = powpow(args[1]);
			} catch (IOException e) {
				System.out.println("Could'nt find the input file. Are you sure, you placed it in ..//files/?");
				return;
			}
		}
		
		if(args[0].equals("KMP"))
		{
			KMP kmp = new KMP(inputString,pattern);
			kmp.mainz();
		}
		else if(args[0].equals("BM"))
		{
			BM bmz = new BM(inputString,pattern);
			bmz.calculateStuff();
		}
		else
		{
			System.out.println("The program needs 3 arguments:");
			System.out.println("Argument 1 should be either KMP or BM");
			System.out.println("Argument 2 should be the file containing the string");
			System.out.println("Argument 3 should be the pattern to search for");
		}
	}
	
	public static String powpow(String argString) throws IOException
	{
		String fileString = "..//files/"+argString;
		File f = new File(fileString);
		InputStream in = new FileInputStream(f);
		int b;
		char s;
		String output = "";
		while((b = in.read()) != -1)
		{
			s = (char)b;
			if(b != 13) //jeg vil ikke have linieskift med
			{
			output = output + s;
			}
		}

		in.close();
		return output;
	}
}
